package edu.vt.ece.rt.benchmark.intruder;


public class Preprocessor {

	/* =============================================================================
	 * Preprocessor_convertURNHex
	 * -- Translate % hex escape sequences
	 * =============================================================================
	 */
	static String 
	Preprocessor_convertURNHex (String str)
	{
	    String src = str;
	    String dst = "";
	    

	    int offset = (int)'a' - (int)'A';
	    for (int i = 0; i < str.length(); i++) {
	    	char c = str.charAt(i);
	    	String curr;
	    	if (c == '%') {
	            String hex = str.substring(i+1, i+3);
//	            assert(hex[1]);
//	            hex[2] = '\0';
//	            int i;
	            hex = hex.toLowerCase();
	            System.out.println("Curr hex is "+hex);
	            int n = Integer.parseInt(hex, 16);
	            System.out.println("Integer is "+n);
//	            assert(n == 1);
//	            src += 2;
//	            *src = (String)i;
	            char currChar = (char)n;
	            curr = currChar+"";
	            System.out.println("Character is "+curr);
	            i = i + 2;
	        } else {
	        	curr = c+"";
	        }
	    	
	    	dst = dst + curr;
	    }
	    
	    return dst;

	}


	/* =============================================================================
	 * Preprocessor_toLower
	 * -- Translate uppercase letters to lowercase
	 * =============================================================================
	 */
	static String
	Preprocessor_toLower (String str)
	{
	    str = str.toLowerCase();
	    return str;
	}


	/* #############################################################################
	 * TEST_PREPROCESSOR
	 * #############################################################################
	 */
	public static void main (String args[]) {
	    System.out.println("Starting...");

	    String hex = "This%20is %41 test%3F%3f";
	    hex = Preprocessor_convertURNHex(hex);
	    assert(hex.equals("This is A test??"));

	    String caps = "ThiS is A tEsT??";
	    hex = Preprocessor_toLower(caps);
	    System.out.println("curr "+hex);
	    assert(hex.equals("this is a test??"));

	    System.out.println("All tests passed.");

	}

}
